package com.pay.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.pay.config.alipay.AlipayConfig;
import com.pay.model.interface_parameter.AliPayQueryParam;
import com.pay.model.interface_return.*;
import com.pay.service.inter.AlipayService;
import com.pay.utils.CodeEnum;
import com.pay.utils.validate.vali_util.ValidateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * 阿里支付
 *
 * 网关 (gateway)，
 *
 * 应用 id (app_id)，
 *
 * 应用私钥 (private_key)，
 *
 * 应用公钥证书路径（app_cert_path 文件绝对路径），
 *
 * 支付宝公钥证书文件路径（alipay_cert_path 文件绝对路径），
 *
 * 支付宝CA根证书文件路径（alipay_root_cert_path 文件绝对路径），
 *
 * 编码格式 （charset），
 *
 * 签名类型 （sign_type）
 *
 * @author MENG
 * @version 2018/7/5
 * @see
 */
@Service("alipayService")
public class AlipayServiceImpl implements AlipayService
{
    Logger logger = LoggerFactory.getLogger(AlipayServiceImpl.class);

    @Value("${app_cert_path}")
    private String app_cert_path;

    @Value("${alipay_cert_path}")
    private String alipay_cert_path;

    @Value("${alipay_root_cert_path}")
    private String alipay_root_cert_path;

    @Override
    public Map<String, Object> getAliPayPcPagePay(String appId, String privateKey,
                                                  String notifyUrl, String orderNo,
                                                  String totalAmount, String subject,
                                                  String qrCodeTimeoutExpress, String returnUrl)
    {
        AliPayPcPagePayReturn aliPayPcPagePayReturn = new AliPayPcPagePayReturn();

        //订单金额
        aliPayPcPagePayReturn.setTotalAmount(totalAmount);

        //订单号
        aliPayPcPagePayReturn.setOrderNo(orderNo);

        //最晚付款时间
        aliPayPcPagePayReturn.setQrCodeTimeoutExpress(qrCodeTimeoutExpress);

        try
        {

            CertAlipayRequest certAlipayRequest = new CertAlipayRequest();

            certAlipayRequest.setServerUrl(AlipayConfig.gatewayUrl);

            certAlipayRequest.setAppId(appId);

            certAlipayRequest.setPrivateKey(privateKey);

            certAlipayRequest.setFormat("json");

            certAlipayRequest.setCharset(AlipayConfig.charset);

            certAlipayRequest.setSignType(AlipayConfig.sign_type);

            certAlipayRequest.setCertPath(app_cert_path);

            certAlipayRequest.setAlipayPublicCertPath(alipay_cert_path);

            certAlipayRequest.setRootCertPath(alipay_root_cert_path);

            DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);

            //设置请求参数
            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();

            request.setNotifyUrl(notifyUrl);

            request.setReturnUrl(returnUrl);

            request.setBizContent("{\"out_trade_no\":\"" + orderNo + "\","
                                  + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\","
                                  + "\"total_amount\":\"" + totalAmount + "\","
                                  + "\"subject\":\"" + subject + "\","
                                  + "\"qr_code_timeout_express\":\""+qrCodeTimeoutExpress+"m\"}");

            AlipayTradePagePayResponse response = null;

            response = alipayClient.pageExecute(request);

            if(response.isSuccess())
            {
                String content = response.getBody();

                aliPayPcPagePayReturn.setQrCodeContent(content);

                return ValidateUtil.returnSuccess(aliPayPcPagePayReturn);
            }
            else
            {
                logger.error("调用阿里电脑网站支付的支付接口API失败.");

                logger.error("错误代码 sub_code : " + response.getSubCode());

                logger.error("错误信息 sub_msg : " + response.getSubMsg());

                return ValidateUtil.returnError( CodeEnum.INVOKE_ALIPAY_PAGE_PAY_FAILE.getCode(),CodeEnum.INVOKE_ALIPAY_PAGE_PAY_FAILE.getMessage() +  " SUB_CODE : " + response.getSubCode() + " SUB_MESSGAE : " + response.getSubMsg());
            }
        }
        catch (Exception e)
        {
            logger.error("阿里电脑网站支付的支付接口错误.", e);

            return ValidateUtil.returnError( CodeEnum.ALIPAY_TRADE_PRECREATE_ERROR.getCode(),CodeEnum.ALIPAY_TRADE_PRECREATE_ERROR.getMessage() + e.getMessage());
        }
    }

    @Override
    public Map<String, Object> aliPayQuery(AliPayQueryParam aliPayQueryParam)
        throws AlipayApiException
    {
        AliPayQueryReturn aliPayQueryReturn = new AliPayQueryReturn();

        aliPayQueryReturn.setOrderNo(aliPayQueryParam.getOrderNo());

        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();

        certAlipayRequest.setServerUrl(AlipayConfig.gatewayUrl);

        certAlipayRequest.setAppId(aliPayQueryParam.getAppId());

        certAlipayRequest.setPrivateKey(aliPayQueryParam.getPrivateKey());

        certAlipayRequest.setFormat("json");

        certAlipayRequest.setCharset(AlipayConfig.charset);

        certAlipayRequest.setSignType(AlipayConfig.sign_type);

        certAlipayRequest.setCertPath(app_cert_path);

        certAlipayRequest.setAlipayPublicCertPath(alipay_cert_path);

        certAlipayRequest.setRootCertPath(alipay_root_cert_path);

        DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);

        AlipayTradeQueryRequest aliPayRequest = new AlipayTradeQueryRequest();

        aliPayRequest.setBizContent("{" +
                                    "\"out_trade_no\":\""+aliPayQueryParam.getOrderNo()+"\"" +
//                                    "\"out_trade_no\":\""+aliPayQueryParam.getOrderNo()+"\"," +
//                                    "\"trade_no\":\"\"" +
                                    "  }");
//        aliPayRequest.setBizContent("{" +
//                                   "\"out_trade_no\":\"377946173497020416\"," +
//                                   "\"trade_no\":\"\"" +
//                                   "  }");

        AlipayTradeQueryResponse response = null;

        try
        {
            response = alipayClient.certificateExecute(aliPayRequest);

            if(response.isSuccess())
            {
                if ("10000".equals(response.getCode()))
                {
                    String trade_status = response.getTradeStatus();

                    if("TRADE_FINISHED".equals(trade_status) || "TRADE_SUCCESS".equals(trade_status))
                    {
                        aliPayQueryReturn.setPayStatus("1");

                        return ValidateUtil.returnSuccess(aliPayQueryReturn);
                    }
                    else if(("WAIT_BUYER_PAY").equals(trade_status))
                    {
                        aliPayQueryReturn.setPayStatus("2");

                        return ValidateUtil.returnSuccess(aliPayQueryReturn);
                    }
                    else
                    {
                        aliPayQueryReturn.setPayStatus("3");

                        return ValidateUtil.returnSuccess(aliPayQueryReturn);
                    }
                }
                else
                {
                    logger.error("支付宝查询订单支付状态失败.");

                    return ValidateUtil.returnError( CodeEnum.ALIPAY_QUERY_ORDER_STATUS_FAILE.getCode(),CodeEnum.ALIPAY_QUERY_ORDER_STATUS_FAILE.getMessage()+response.getSubMsg());
                }
            }
            else
            {
                logger.error("调用支付宝查询订单支付状态API失败.");

                logger.error("错误代码 sub_code : " + response.getSubCode());

                logger.error("错误信息 sub_msg : " + response.getSubMsg());

                return ValidateUtil.returnError( CodeEnum.INVOKE_ALIPAY_TRADE_PRECREATE_FAILE.getCode(),CodeEnum.INVOKE_ALIPAY_TRADE_PRECREATE_FAILE.getMessage()+response.getSubMsg());
            }
        }
        catch (AlipayApiException e)
        {
            logger.error("支付宝支付查询错误",e);

            return ValidateUtil.returnError(CodeEnum.ALIPAY_QUERY_ORDER_STATUS_ERROR.getCode(),CodeEnum.ALIPAY_QUERY_ORDER_STATUS_ERROR.getMessage());
        }
    }


    public Map<String,Object> scanPay(String appId, String merchantPrivateKey,
                                      String notifyUrl, String outTradeNo, String totalAmount, String subject, String qrCodeTimeoutExpress)
    {
        AliPayQrCodeReturn aliPayQrCodeReturn = new AliPayQrCodeReturn();

        //订单金额
        aliPayQrCodeReturn.setTotalAmount(totalAmount);

        //订单号
        aliPayQrCodeReturn.setOrderNo(outTradeNo);

        //二维码失效时间
        aliPayQrCodeReturn.setQrCodeTimeoutExpress(qrCodeTimeoutExpress);

        try
        {
            //获得初始化的AlipayClient
            AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, appId, merchantPrivateKey, "json", AlipayConfig.charset,  AlipayConfig.sign_type);

            //设置请求参数
            AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();

            request.setNotifyUrl(notifyUrl);

            request.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","
                                  + "\"total_amount\":\"" + totalAmount + "\","
                                  + "\"subject\":\"" + subject + "\","
                                  + "\"qr_code_timeout_express\":\""+qrCodeTimeoutExpress+"m\"}");

            AlipayTradePrecreateResponse response = null;

            response = alipayClient.execute(request);

            if(response.isSuccess())
            {
                if ("10000".equals(response.getCode()))
                {
                    logger.info("阿里扫码预创建交易订单API调用成功");

                    //接口调用成功
                    //商户传过来的唯一订单号
                    String no = response.getOutTradeNo();

                    String content = response.getQrCode();

                    aliPayQrCodeReturn.setQrCodeContent(content);

                    return ValidateUtil.returnSuccess(aliPayQrCodeReturn);
                }
                else
                {
                    logger.error("阿里扫码预创建交易订单失败.");

                    return ValidateUtil.returnError( CodeEnum.ALIPAY_TRADE_PRECREATE_FAILE.getCode(),CodeEnum.ALIPAY_TRADE_PRECREATE_FAILE.getMessage()+response.getSubMsg());
                }
            }
            else
            {
                logger.error("调用阿里扫码预创建交易订单API失败.");

                logger.error("错误代码 sub_code : " + response.getSubCode());

                logger.error("错误信息 sub_msg : " + response.getSubMsg());

                return ValidateUtil.returnError( CodeEnum.INVOKE_ALIPAY_TRADE_PRECREATE_FAILE.getCode(),CodeEnum.INVOKE_ALIPAY_TRADE_PRECREATE_FAILE.getMessage()+response.getSubMsg());
            }
        }
        catch (Exception e)
        {
            logger.error("阿里扫码预创建交易订单错误.", e);

            return ValidateUtil.returnError( CodeEnum.ALIPAY_TRADE_PRECREATE_ERROR.getCode(),CodeEnum.ALIPAY_TRADE_PRECREATE_ERROR.getMessage() + e.getMessage());
        }
    }


    @Override
    public Map<String,Object> alipayCode(String appId, String merchantPrivateKey,
                                         String notifyUrl, String outTradeNo, String authCode,
                                         String subject, String totalAmount,
                                         String timeoutExpress)
    {
        //返回结果
        AliPayAuthCodeReturn aliPayAuthCodeReturn = new AliPayAuthCodeReturn();

        //订单号
        aliPayAuthCodeReturn.setOrderNo(outTradeNo);

        //订单金额
        aliPayAuthCodeReturn.setTotalAmount(totalAmount);

        //订单最后付款时间
        aliPayAuthCodeReturn.setTimeoutExpress(timeoutExpress);

        try
        {
            //获得初始化的AlipayClient
            AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, appId, merchantPrivateKey, "json", AlipayConfig.charset, AlipayConfig.sign_type);

            AlipayTradePayRequest request = new AlipayTradePayRequest();

            request.setNotifyUrl(notifyUrl);

            request.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","
                                  + "\"scene\":\"bar_code\","
                                  + "\"total_amount\":\"" + totalAmount + "\","
                                  + "\"subject\":\"" + subject + "\","
                                  + "\"timeout_express\":\"" + timeoutExpress + "m\","
                                  + "\"auth_code\":\"" + authCode + "\"}");


            AlipayTradePayResponse response = null;

            response = alipayClient.execute(request);

            if(response.isSuccess())
            {
                if ("10000".equals(response.getCode()))
                {
                    logger.info("阿里付款码支付API调用成功");

                    //支付成功
                    aliPayAuthCodeReturn.setPayStatus("1");

                    //支付宝交易号
//                    aliPayAuthCodeReturn.setTradeNo(response.getTradeNo());

                    return ValidateUtil.returnSuccess(aliPayAuthCodeReturn);
                }
                else
                {
                    //支付中
                    if("10003".equals(response.getCode()))
                    {
                        logger.info("阿里付款码交易支付中...");

                        //支付中
                        aliPayAuthCodeReturn.setPayStatus("2");

                        return ValidateUtil.returnSuccess(aliPayAuthCodeReturn);
                    }
                    else
                    {
                        logger.error("错误代码 code : " + response.getCode());

                        logger.error("错误信息 msg : " + response.getMsg());

                        logger.error("错误代码 sub_code : " + response.getSubCode());

                        logger.error("错误信息 error_msg : " + response.getSubMsg());

                        logger.error("阿里付款码交易支付接口失败.");

                        return ValidateUtil.returnError(CodeEnum.ALIPAY_TRADE_PAY_FAILE.getCode(),CodeEnum.ALIPAY_TRADE_PAY_FAILE.getMessage()+response.getSubMsg());
                    }
                }
            }
            else
            {
                logger.error("调用阿里付款码交易支付接口API失败.");

                logger.error("错误代码 code : " + response.getCode());

                logger.error("错误信息 msg : " + response.getMsg());

                logger.error("错误代码 sub_code : " + response.getSubCode());

                logger.error("错误信息 error_msg : " + response.getSubMsg());

                return ValidateUtil.returnError(CodeEnum.INVOKE_ALIPAY_TRADE_PAY_FAILE.getCode(),CodeEnum.INVOKE_ALIPAY_TRADE_PAY_FAILE.getMessage()+response.getSubMsg());
            }
        }
        catch (AlipayApiException e)
        {
            logger.error("阿里付款码交易支付接口错误!", e);

            return ValidateUtil.returnError(CodeEnum.ALIPAY_TRADE_PAY_ERROR.getCode(),CodeEnum.ALIPAY_TRADE_PAY_ERROR.getMessage()+e.getMessage());
        }
    }

    @Override
    public Map<String, Object> alipayRefund(String appId, String merchantPrivateKey,
                                            String outTradeNo,
                                            String refundAmount,String refundReason)
    {
        //返回结果
        AliPayRefundReturn aliPayRefundReturn = new AliPayRefundReturn();

        //订单号
        aliPayRefundReturn.setOrderNo(outTradeNo);

        //退款金额
        aliPayRefundReturn.setRefundAmount(refundAmount);

        //退款原因
        aliPayRefundReturn.setRefundReason(refundReason);

        try
        {
            CertAlipayRequest certAlipayRequest = new CertAlipayRequest();

            certAlipayRequest.setServerUrl(AlipayConfig.gatewayUrl);

            certAlipayRequest.setAppId(appId);

            certAlipayRequest.setPrivateKey(merchantPrivateKey);

            certAlipayRequest.setFormat("json");

            certAlipayRequest.setCharset(AlipayConfig.charset);

            certAlipayRequest.setSignType(AlipayConfig.sign_type);

            certAlipayRequest.setCertPath(app_cert_path);

            certAlipayRequest.setAlipayPublicCertPath(alipay_cert_path);

            certAlipayRequest.setRootCertPath(alipay_root_cert_path);

            DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
            //获得初始化的AlipayClient
//            AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, appId, merchantPrivateKey, "json", AlipayConfig.charset, alipayPublicKey, AlipayConfig.sign_type);

            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();

            request.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","
//                                  + "\"trade_no\":\"" + tradeNo + "\","
                                  + "\"refund_amount\":\"" + refundAmount + "\","
                                  + "\"refund_reason\":\"" + refundReason + "\"}");

            AlipayTradeRefundResponse response = alipayClient.certificateExecute(request);

            if(response.isSuccess())
            {
                if ("10000".equals(response.getCode()))
                {
                    logger.info("阿里支付退款API调用成功");

                    return ValidateUtil.returnSuccess(aliPayRefundReturn);
                }
                else
                {
                    logger.error("阿里支付退款接口失败");

                    logger.error("错误代码 sub_code : " + response.getSubCode());

                    logger.error("错误信息 error_msg : " + response.getSubMsg());

                    return ValidateUtil.returnError(CodeEnum.ALIPAY_TRADE_REFUND_FAILE.getCode(),CodeEnum.ALIPAY_TRADE_REFUND_FAILE.getMessage()+response.getSubMsg());
                }
            }
            else
            {
                logger.error("调用阿里支付退款接口API失败");

                logger.error("错误代码 sub_code : " + response.getSubCode());

                logger.error("错误信息 error_msg : " + response.getSubMsg());

                return ValidateUtil.returnError(CodeEnum.INVOKE_ALIPAY_TRADE_REFUND_FAILE.getCode(),CodeEnum.INVOKE_ALIPAY_TRADE_REFUND_FAILE.getMessage()+response.getSubMsg());
            }

        }
        catch (Exception e)
        {
            logger.error("阿里支付退款接口错误!", e);

            return ValidateUtil.returnError(CodeEnum.ALIPAY_TRADE_REFUND_ERROR.getCode(),CodeEnum.ALIPAY_TRADE_REFUND_ERROR.getMessage() + e.getMessage());
        }
    }

    @Override
    public Map<String, Object> aliPayRebate(String appId, String merchantPrivateKey, String outTradeNo,
                                            String refundAmount, String refundReason,
                                            String totalAmount, String sunOrderCode)
    {
        //返回结果
        AliPayRefundReturn aliPayRefundReturn = new AliPayRefundReturn();

        //订单号
        aliPayRefundReturn.setOrderNo(outTradeNo);

        //退款金额
        aliPayRefundReturn.setRefundAmount(refundAmount);

        //退款原因
        aliPayRefundReturn.setRefundReason(refundReason);

        try
        {
            CertAlipayRequest certAlipayRequest = new CertAlipayRequest();

            certAlipayRequest.setServerUrl(AlipayConfig.gatewayUrl);

            certAlipayRequest.setAppId(appId);

            certAlipayRequest.setPrivateKey(merchantPrivateKey);

            certAlipayRequest.setFormat("json");

            certAlipayRequest.setCharset(AlipayConfig.charset);

            certAlipayRequest.setSignType(AlipayConfig.sign_type);

            certAlipayRequest.setCertPath(app_cert_path);

            certAlipayRequest.setAlipayPublicCertPath(alipay_cert_path);

            certAlipayRequest.setRootCertPath(alipay_root_cert_path);

            DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
            //获得初始化的AlipayClient
//            AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, appId, merchantPrivateKey, "json", AlipayConfig.charset, alipayPublicKey, AlipayConfig.sign_type);

            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();

            request.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","
//                                  + "\"trade_no\":\"" + tradeNo + "\","
                                  + "\"out_request_no\":\"" + sunOrderCode + "\","
                                  + "\"refund_amount\":\"" + refundAmount + "\","
                                  + "\"refund_reason\":\"" + refundReason + "\"}");

            AlipayTradeRefundResponse response = alipayClient.certificateExecute(request);

            if(response.isSuccess())
            {
                if ("10000".equals(response.getCode()))
                {
                    logger.info("阿里支付退款API调用成功");

                    return ValidateUtil.returnSuccess(aliPayRefundReturn);
                }
                else
                {
                    logger.error("阿里支付退款接口失败");

                    logger.error("错误代码 sub_code : " + response.getSubCode());

                    logger.error("错误信息 error_msg : " + response.getSubMsg());

                    return ValidateUtil.returnError(CodeEnum.ALIPAY_TRADE_REFUND_FAILE.getCode(),CodeEnum.ALIPAY_TRADE_REFUND_FAILE.getMessage()+response.getSubMsg());
                }
            }
            else
            {
                logger.error("调用阿里支付退款接口API失败");

                logger.error("错误代码 sub_code : " + response.getSubCode());

                logger.error("错误信息 error_msg : " + response.getSubMsg());

                return ValidateUtil.returnError(CodeEnum.INVOKE_ALIPAY_TRADE_REFUND_FAILE.getCode(),CodeEnum.INVOKE_ALIPAY_TRADE_REFUND_FAILE.getMessage()+response.getSubMsg());
            }

        }
        catch (Exception e)
        {
            logger.error("阿里支付退款接口错误!", e);

            return ValidateUtil.returnError(CodeEnum.ALIPAY_TRADE_REFUND_ERROR.getCode(),CodeEnum.ALIPAY_TRADE_REFUND_ERROR.getMessage() + e.getMessage());
        }
    }


    @Override
    public Map<String, Object> alipayMobileWeb(String appId, String privateKey,
                                               String notifyUrl, String orderNo,
                                               String subject,
                                               String totalAmount, String timeoutExpress,
                                               String returnUrl)
    {
        //返回结果
        AliPayMobileWebReturn aliPayMobileWebReturn = new AliPayMobileWebReturn();

        //订单号
        aliPayMobileWebReturn.setOrderNo(orderNo);

        //订单金额
        aliPayMobileWebReturn.setTotalAmount(totalAmount);

        //订单最后付款时间
        aliPayMobileWebReturn.setTimeoutExpress(timeoutExpress);

        logger.info("===========  app_cert_path : " + Thread.currentThread().getContextClassLoader().getResource("static/alipay_cert/app_cert_path.crt").getPath());

        try
        {
            CertAlipayRequest certAlipayRequest = new CertAlipayRequest();

            certAlipayRequest.setServerUrl(AlipayConfig.gatewayUrl);

            certAlipayRequest.setAppId(appId);

            certAlipayRequest.setPrivateKey(privateKey);

            certAlipayRequest.setFormat("json");

            certAlipayRequest.setCharset(AlipayConfig.charset);

            certAlipayRequest.setSignType(AlipayConfig.sign_type);

            certAlipayRequest.setCertPath(app_cert_path);

            certAlipayRequest.setAlipayPublicCertPath(alipay_cert_path);

            certAlipayRequest.setRootCertPath(alipay_root_cert_path);

            DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);

            //获得初始化的AlipayClient
//            AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, appId, privateKey, "json", AlipayConfig.charset, publicKey, AlipayConfig.sign_type);

            AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();

            request.setNotifyUrl(notifyUrl);

            request.setReturnUrl(returnUrl);

            request.setBizContent("{\"out_trade_no\":\"" + orderNo + "\","
                                  + "\"total_amount\":\"" + totalAmount + "\","
                                  + "\"quit_url\":\"" + returnUrl + "\","
                                  + "\"timeout_express\":\"" + timeoutExpress + "m\","
                                  + "\"product_code\":\"QUICK_WAP_WAY\","
                                  + "\"subject\":\"" + subject + "\"}");


            AlipayTradeWapPayResponse response = null;

            response = alipayClient.pageExecute(request);

            if(response.isSuccess())
            {
//                if ("10000".equals(response.getCode()))
//                {
                    logger.info("阿里手机H5支付API调用成功");

                    aliPayMobileWebReturn.setForm(response.getBody());

                    return ValidateUtil.returnSuccess(aliPayMobileWebReturn);
//                }
//                else
//                {
//                    logger.error("阿里手机H5支付接口失败");
//
//                    logger.error("错误代码 sub_code : " + response.getSubCode());
//
//                    logger.error("错误信息 error_msg : " + response.getSubMsg());
//
//                    return ValidateUtil.returnError(CodeEnum.ALIPAY_TRADE_WAP_PAY_FAIL.getCode(),CodeEnum.ALIPAY_TRADE_WAP_PAY_FAIL.getMessage()+response.getSubMsg());
//                }
            }
            else
            {
                logger.error("调用阿里手机H5支付接口API失败");

                logger.error("错误代码 sub_code : " + response.getSubCode());

                logger.error("错误信息 error_msg : " + response.getSubMsg());

                return ValidateUtil.returnError(CodeEnum.INVOKE_ALIPAY_TRADE_WAP_PAY_FAIL.getCode(),CodeEnum.INVOKE_ALIPAY_TRADE_WAP_PAY_FAIL.getMessage()+response.getSubMsg());
            }
        }
        catch (AlipayApiException e)
        {
            logger.error("阿里手机H5支付接口错误!", e);

            return ValidateUtil.returnError(CodeEnum.ALIPAY_TRADE_WAP_PAY_ERROR.getCode(),CodeEnum.ALIPAY_TRADE_WAP_PAY_ERROR.getMessage()+e.getMessage());
        }
    }




}
